import service from './service'
import config from './config'
import type {AxiosRequestConfig, AxiosResponse} from 'axios'

const {default_content_type} = config

const request = <D = any>(option: AxiosRequestConfig<D>) => {
	if (!option.headers) {
		option.headers = {'Content-Type': default_content_type}
	} else if (!option.headers['Content-Type']) {
		option.headers['Content-Type'] = default_content_type
	}
	return service(option)
}

export default {
	get: async <T = any, D = any>(option: AxiosRequestConfig<D>): Promise<ApiResult<T>> => {
		const res = await request({...option, method: 'GET'})
		return res.data as Promise<ApiResult<T>>
	},
	post: async <T = any, D = any>(option: AxiosRequestConfig<D>): Promise<ApiResult<T>> => {
		const res = await request({...option, method: 'POST'})
		return res.data as Promise<ApiResult<T>>
	},
	delete: async <T = any, D = any>(option: AxiosRequestConfig<D>): Promise<ApiResult<T>> => {
		const res = await request({...option, method: 'DELETE'})
		return res.data as Promise<ApiResult<T>>
	},
	put: async <T = any, D = any>(option: AxiosRequestConfig<D>): Promise<ApiResult<T>> => {
		const res = await request({...option, method: 'PUT'})
		return res.data as Promise<ApiResult<T>>
	},
	getOriginal: async <T = any, D = any>(option: AxiosRequestConfig<D>) => {
		return await request({...option, method: 'GET'}) as unknown as Promise<AxiosResponse<T, D>>
	},
	postOriginal: async <T = any, D = any>(option: AxiosRequestConfig<D>) => {
		return await request({...option, method: 'POST'}) as unknown as Promise<AxiosResponse<T, D>>
	},
	deleteOriginal: async <T = any, D = any>(option: AxiosRequestConfig<D>) => {
		return await request({...option, method: 'DELETE'}) as unknown as Promise<AxiosResponse<T, D>>
	},
	putOriginal: async <T = any, D = any>(option: AxiosRequestConfig<D>) => {
		return await request({...option, method: 'PUT'}) as unknown as Promise<AxiosResponse<T, D>>
	}
}
